<?php
namespace jy_area_dai;
defined('BY_JYA') or exit('error');
/*
	经销插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function otherfee_types(){
		$jy_renwushop = check_permission('jy_renwushop');
		$res=array(
			'0'=>'无',
		);
		if($jy_renwushop){
			$res['jy_renwushop']='任务商城分佣';
		}
		return $res;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if(!$isup){
				require ROOT_D.'/update/update_area.php';
				$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
			}else{
				$area_code=pdo_getcolumn('core_area',array('address'=>$oldaddress),'code');
				if(!$area_code){
					pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_area;');
					require ROOT_D.'/update/update_area.php';
				}
			}
		}
		return $area_code;
	}
	public function get_upgoods($goods){
		$member=$this->get('member',array('mid'=>$_SESSION['uid']),array('mid','level_id','mid','is_agent','agent_id','sale_id'));
		if($member['is_agent']){
			$mygoods=$this->get('goods',array('goods_id'=>$goods['id']));
			if(!$mygoods['id']){
				return $goods;
			}
			if($mygoods['diyprice']){
				$diyprice=json_decode($mygoods['diyprice'],true);
				foreach($diyprice as $dv){
					if($member['level_id']==$dv['levelid']){
						$goods['price']=$dv['price'];
					}
				}
			}
		}
		return $goods;
	}
	public function add_upfee(){
		if($this->setting['yuered_open']!=1){
			exi('分销设置--分红--未开启《优秀店主极差分红》！','error');
		}
		$end_time=strtotime(date("Y-m",time()));
		//$end_time=time();
		$start_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
		$this->month=date("Y-m",$start_time+200);
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$end_time);
		$parameter=array(
			'params'=>$params,
			'limit'=>'1',
			'orderby'=>'id desc',
		);
		$list=$this->get_list('upfee',$parameter,$total);
		if(count($list)>0){
			exi('上月的分红已经派发！','error');
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($start_time,$end_time));
		$levels=$this->getall('level',array('yeji_open'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		if($levels){
			$level_ids=array_keys($levels);
		}else{
			exi('没人任何店主等级--支持《优秀店主极差分红》！','error');
		}
		//全部分销商
		$members=$this->getall('member',array('level_id'=>$level_ids,'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1));
		foreach($members as $item){
			$this->xun_upfee($item,$params,$level_ids);
		}
		return true;
	}
	/*
		循环
	*/
	public function xun_upfee($member=array(),$params=array(),$level_ids=array()){
		$params['mid']=$member['mid'];
		//计算进货额度
		$order_price=$this->getsum('order',$params,'order_price');
		if($order_price<1){
			return false;
		}
		$yueji_fee=$this->yueji_fee($order_price);
		if(!$yueji_fee){
			return false;
		}
		$insert=array(
				'mid'=>$params['mid'],
				'n_mid'=>0,
				'rate'=>$yueji_fee['rate'],
				'manfee'=>$yueji_fee['manfee'],
				'ji_fee'=>$order_price,
				'ji_rate'=>$yueji_fee['rate'],
				'month'=>$this->month,
				'agent_id'=>$member['agent_id'],
		);
		$this->set_upfee($insert);
		/*foreach($n_members as $nv1){
						$params['mid']=$nv1['mid'];
						$n_price=$this->getsum('order',$params,'order_price');
						if($n_price<1){
							continue;
						}
						$n_yueji_fee=$this->yueji_fee($n_price);
						if(!$n_yueji_fee){
							continue;
						}
						$ji_rate=$yueji_fee['rate']-$n_yueji_fee['rate'];
						$ji_fee=$order_price-$n_price;
						$insert=array(
							'mid'=>$params['mid'],
							'n_mid'=>$nv1['mid'],
							'rate'=>$yueji_fee['rate'],
							'manfee'=>$yueji_fee['manfee'],
							'ji_fee'=>$ji_fee,
							'ji_rate'=>$yueji_fee['rate'],
							'month'=>$this->month,
						);
						$this->set_upfee($insert);
						$this->xun_upfee($nv1,$params);
		}*/
		return true;
	}
	/*
		获取阶梯
	*/
	public function yueji_fee($fee=0){
		if($fee<1){
			return false;
		}
		$yueji_fee=$this->setting['yueji_fee'];
		paixu($yueji_fee,'manfee','desc');
		foreach($yueji_fee as $v){
			if($fee>=$v['manfee']){
				return $v;
			}
		}
		return false;
	}
	/*
		生成月度费
	*/
	public function set_upfee($params,$type=0){
		$money=$params['ji_fee']*$params['ji_rate']/100;
		if($money<=0){
			return false;
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'n_mid'=>$params['n_mid'],
			'rate'=>$params['rate'],
			'manfee'=>$params['manfee'],
			'ji_fee'=>$params['ji_fee'],
			'ji_rate'=>$params['ji_rate'],
			'money'=>$money,
			'month'=>$params['month'],
			'type'=>$type,
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('upfee',$insert);
		$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$params['mid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>28,
					'dian_mid'=>$params['agent_id'],
					'send_feetype'=>$params['agent_id']?1:0,
		);
		$this->pdo_insert('commission',$insert);
		return true;
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'dian_mid'=>$params['dian_mid'],
			'msg'=>$params['msg'],
			'type'=>$type,
			'fee'=>$params['fee'],
			'status'=>$params['status'],
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('log',$insert);
		return true;
	}
	/*
		经销商品库存
	*/
	protected function check_buy($goodsid,$num=1,$optionsid=0){
		$member=$this->get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$member['agent_id']){
			model('jy_weishop/goods')->check_buy($goodsid,$num,$optionsid);
			return true;
		}
		$mygoods=$this->get('mygoods',array('mid'=>$member['agent_id'],'goodsid'=>$goodsid));
		if(!$mygoods['id']){
			exi('该商品还未入库，请联系您的经销商！','error');
		}
		if($mygoods['id'] && $mygoods['total']<$num){
			exi('该商品库存不足，请联系您的经销商！','error');
		}
		return true;
	}
}